//
//  YHHomeAgentScreenMenuView.m
//  YHApp
//
//  Created by -- on 2022/12/3.
//

#import "HAHomeAgentScreenPopView.h"
#import "HACanalItemMo.h"
#import "YWAlertView.h"
#import "HAAgentLevelsMo.h"

@interface HAHomeAgentScreenPopView ()<UITextFieldDelegate>

/***/
@property (nonatomic, strong) UITextField *seachField;
/***/
@property (nonatomic, strong) UILabel *startTimeLabel;
/***/
@property (nonatomic, strong) UILabel *endTimeLabel;

@property (nonatomic,strong) id <YWAlertViewProtocol>dateAlert;
/***/
@property (nonatomic, assign) NSInteger timeType;

/***/
@property (nonatomic, strong) NSMutableArray *agentLevelBtnList;

@end


@implementation HAHomeAgentScreenPopView

-(instancetype)init {
    if (self = [super init]) {

    }
    return self;

}

-(void)seachBtnClick {
    [self sureButtonClick];
}

-(void)time1Click {
    self.timeType = 1;
    [self.dateAlert show];
}

-(void)time2Click {
    self.timeType = 2;
    [self.dateAlert show];
}

- (id<YWAlertViewProtocol>)dateAlert{
    if (!_dateAlert) {
        WS(weakSelf)
        _dateAlert = [YWAlertView alertViewWithTitle:@"请选择日期" preferredStyle:YWAlertViewStyleDatePicker2 footStyle:YWAlertPublicFootStyleDefalut bodyStyle:YWAlertStyleShowYearMonthDay cancelButtonTitle:@"取消" sureButtonTitles:@"确定" handler:^(NSInteger buttonIndex, id  _Nullable value) {
            
            if (!value) {
                return;
            }
            if (weakSelf.timeType == 1) {
                weakSelf.startTimeLabel.text = value;
            }
            if (weakSelf.timeType == 2) {
                weakSelf.endTimeLabel.text = value;
            }

        }];
    }
    return _dateAlert;
}


-(void)buttonClick:(UIButton *)button {
    HACanalItemMo *model = [HASingle single].canalItemArray[button.tag];
    model.isSel = !model.isSel;
    if (model.isSel) {
        [button setTitleColor:[UIColor colorWithHexString:@"#FFC4AC"] forState:UIControlStateNormal];
        button.layer.borderColor = [UIColor colorWithHexString:@"#FFC4AC"].CGColor;
    }else {
        [button setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        button.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    }
}

-(void)buttonAgentLevelClick:(UIButton *)button {
    for (HAAgentLevelsMo *mo in self.agentLevelList) {
        mo.isSel = NO;
    }
    for (UIButton *button in self.agentLevelBtnList) {
        [button setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        button.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    }
    
    HAAgentLevelsMo *mo = self.agentLevelList[button.tag];
    mo.isSel = YES;
    [button setTitleColor:[UIColor colorWithHexString:@"#FFC4AC"] forState:UIControlStateNormal];
    button.layer.borderColor = [UIColor colorWithHexString:@"#FFC4AC"].CGColor;
}

-(void)resetButtonClick {
    self.seachField.text = @"";
    
    self.startTimeLabel.text = @"请选择(YYYY-MM-DD)";
    self.endTimeLabel.text = @"请选择(YYYY-MM-DD)";

    for (HAAgentLevelsMo *mo in self.agentLevelList) {
        mo.isSel = NO;
    }
    for (UIButton *button in self.agentLevelBtnList) {
        [button setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        button.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    }
    
}

-(void)sureButtonClick {
    [self dismiss];
    
    NSString *searchText = self.seachField.text ?: @"";
    NSString *start = @"";
    NSString *end = @"";
    if (![self.startTimeLabel.text isEqualToString:@"请选择(YYYY-MM-DD)"]) {
        start = self.startTimeLabel.text;
    }
    if (![self.endTimeLabel.text isEqualToString:@"请选择(YYYY-MM-DD)"]) {
        end = self.endTimeLabel.text;
    }
    if (self.YHHomeAgentScreenMenuViewBlock) {
        self.YHHomeAgentScreenMenuViewBlock(searchText, start, end);
    }
    
}

-(void)loadViews {
    
    self.frame = CGRectMake(0, 0, kUIScreen_w, kUIScreen_h);
    [self addSubview:self.backgroundView];
    
    CGFloat alertViewW = kUIScreen_w*0.85;
    CGFloat alertViewH = kUIScreen_h;
    CGFloat alertViewX = kUIScreen_w-alertViewW;
    CGFloat alertViewY = kUIScreen_h-alertViewH;
    self.alertView.frame = CGRectMake(alertViewX, alertViewY, alertViewW, alertViewH);
    [self addSubview: self.alertView];
    self.alertView.backgroundColor = [UIColor whiteColor];
    self.alertView.layer.cornerRadius = 0;
    self.alertView.layer.masksToBounds = YES;
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:18 textColor:@"#333333" text:@"全部筛选" alignment:0];
    [self.alertView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(app_topH+44+12);
        make.left.mas_equalTo(20);
    }];
    
    CGFloat btnY = app_topH+44+28+62;
    
    UIView *lineView = [UIView ha_createView];
    lineView.backgroundColor = [UIColor khaBGColor];
    [self.alertView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(btnY);
        make.height.mas_offset(1);
    }];
    
    UIButton *seachBtn = [[UIButton alloc]init];
    [seachBtn setImage:[UIImage xl_cuttingImageWith:[UIImage imageNamed:@"yh_10"] toSize:CGSizeMake(22, 22)] forState:UIControlStateNormal];
    [seachBtn addTarget:self action:@selector(seachBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:seachBtn];
    [seachBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(lineView);
        make.bottom.mas_equalTo(lineView).offset(-4);
        make.width.mas_offset(32);
        make.height.mas_offset(32);
    }];

    UITextField *seachField = [[UITextField alloc]init];
    self.seachField = seachField;
    seachField.delegate = self;
    seachField.placeholder = @"请输入名称/编号/机具编号";
    seachField.font = [UIFont systemFontOfSize:18];
    [self.alertView addSubview:seachField];
    [seachField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(lineView);
        make.right.equalTo(seachBtn.mas_left).offset(-5);
        make.centerY.mas_equalTo(seachBtn);
        make.height.equalTo(@32);
    }];

    btnY += 20;

    UIView *line2View = [UIView ha_createView];
    line2View.backgroundColor = [UIColor khaBGColor];
    [self.alertView addSubview:line2View];
    [line2View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(btnY+44);
        make.height.mas_offset(1);
    }];
    
    UILabel *tit3Label = [UILabel ha_NLabelWithFont:18 textColor:@"#333333" text:@"开始时间" alignment:0];
    [self.alertView addSubview:tit3Label];
    [tit3Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(line2View.mas_top).offset(-12);
        make.left.mas_equalTo(20);
        make.width.mas_offset(78);
    }];
    
    UILabel *startTimeLabel = [UILabel ha_NLabelWithFont:16 textColor:@"#333333" text:@"请选择(YYYY-MM-DD)" alignment:0];
    self.startTimeLabel = startTimeLabel;
    startTimeLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(time1Click)];
    [startTimeLabel addGestureRecognizer:tap1];
    [self.alertView addSubview:startTimeLabel];
    [startTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(tit3Label);
        make.left.mas_equalTo(tit3Label.mas_right).offset(15);
        make.height.mas_offset(28);
    }];
    
    
    UIView *line3View = [UIView ha_createView];
    line3View.backgroundColor = [UIColor khaBGColor];
    [self.alertView addSubview:line3View];
    [line3View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(btnY+40+50);
        make.height.mas_offset(1);
    }];
    
    UILabel *tit4Label = [UILabel ha_NLabelWithFont:18 textColor:@"#333333" text:@"结束时间" alignment:0];
    [self.alertView addSubview:tit4Label];
    [tit4Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(line3View.mas_top).offset(-8);
        make.left.mas_equalTo(20);
        make.width.mas_offset(78);
    }];
    
    UILabel *endTimeLabel = [UILabel ha_NLabelWithFont:16 textColor:@"#333333" text:@"请选择(YYYY-MM-DD)" alignment:0];
    self.endTimeLabel = endTimeLabel;
    endTimeLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(time2Click)];
    [endTimeLabel addGestureRecognizer:tap2];
    [self.alertView addSubview:endTimeLabel];
    [endTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(tit4Label);
        make.left.mas_equalTo(tit4Label.mas_right).offset(15);
        make.height.mas_offset(28);
    }];
    
    btnY = btnY + 62;
    CGFloat btnX = 20;
    CGFloat btnH = 24;
    CGFloat space = 10;    
    
    if (self.agentLevelList.count > 0) {
        btnY += 32+24;
        UILabel *titLabel = [UILabel ha_NLabelWithFont:18 textColor:@"#333333" text:@"选择等级" alignment:0];
        [self.alertView addSubview:titLabel];
        [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(btnY);
            make.left.mas_equalTo(20);
            make.width.mas_offset(78);
        }];
        btnY += 32;
        btnX = 20;
        [self.agentLevelBtnList removeAllObjects];
        for (int i = 0; i < self.agentLevelList.count; i ++) {
            HAAgentLevelsMo *mo = self.agentLevelList[i];
            UIButton *button = [[UIButton alloc]init];
            if (mo.isSel) {
                [button setTitleColor:[UIColor colorWithHexString:@"#FFC4AC"] forState:UIControlStateNormal];
                button.layer.borderColor = [UIColor colorWithHexString:@"#FFC4AC"].CGColor;
            }else {
                [button setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
                button.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
            }
            [button setTitle:mo.name forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            button.tag= i;
            button.layer.cornerRadius = 8;
            button.layer.masksToBounds = YES;
            button.layer.borderWidth = 1;
            [button addTarget:self action:@selector(buttonAgentLevelClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.alertView addSubview:button];
            CGFloat btnW = [[button currentTitle] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}].width+24;
            if (btnX + btnW + space > alertViewW-12) {
                btnY = btnY + btnH + 10;
                btnX = 20;
            }
            button.frame = CGRectMake(btnX, btnY, btnW, btnH);
            btnX = btnX + btnW + space;//下一个x
            [self.agentLevelBtnList addObject:button];
        }
    }
        
    
    UIButton *resetButton = [[UIButton alloc]init];
    [resetButton setTitle:@"重置" forState:UIControlStateNormal];
    [resetButton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    resetButton.titleLabel.font = [UIFont systemFontOfSize:18];
    resetButton.layer.cornerRadius = 2;
    resetButton.layer.masksToBounds = YES;
    resetButton.layer.borderWidth = 1;
    resetButton.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    [resetButton addTarget:self action:@selector(resetButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:resetButton];
    [resetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(44);
        make.width.mas_offset(120);
        make.top.mas_equalTo(btnY+88);
        make.centerX.mas_equalTo(-70);
    }];
    
    UIButton *sureButton = [[UIButton alloc]init];
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:18];
    sureButton.layer.cornerRadius = 2;
    sureButton.layer.masksToBounds = YES;
    sureButton.layer.borderWidth = 1;
    sureButton.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    sureButton.backgroundColor = [UIColor colorWithHexString:@"#333333"];
    [sureButton addTarget:self action:@selector(sureButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:sureButton];
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(44);
        make.width.mas_offset(120);
        make.top.mas_equalTo(btnY+88);
        make.centerX.mas_equalTo(70);
    }];
    
    if (![NSString isEmptyString:self.start]) {
        self.startTimeLabel.text = self.start;
    }
    if (![NSString isEmptyString:self.end]) {
        self.endTimeLabel.text = self.end;
    }
    
    self.seachField.text = self.keyword;

}


- (NSMutableArray *)agentLevelBtnList
{
    if (!_agentLevelBtnList) {
        _agentLevelBtnList = [NSMutableArray array];
        
    }
    return _agentLevelBtnList;
}


-(void)show {

    UIWindow *keyWindow =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];

    [keyWindow addSubview:self];
    CGRect rect = self.alertView.frame;
    rect.origin.x = kUIScreen_w;
    self.alertView.frame = rect;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.x = kUIScreen_w-kUIScreen_w*0.85;
        self.alertView.frame = rect;
    }];
}
-(void)dismiss {
    [UIView animateWithDuration:0.25 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.x = kUIScreen_w;
        self.alertView.frame = rect;
        self.backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        }
        [self removeFromSuperview];
    }];
}


@end

